package net.googlecode.demenkov.dao.impl;

import net.googlecode.demenkov.dao.FacultyDAO;
import net.googlecode.demenkov.domains.Faculty;
import net.googlecode.demenkov.domains.University;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.List;

/**
 * DAO class for actions with faculties
 *
 * @author Demenkov Yura
 */
@Repository
public class FacultyDAOImpl extends GenericDAOImpl<Faculty, Integer> implements FacultyDAO {
    public Faculty findFacultyByFacultyNameAndUniversity(String facultyName, University university) {
        Query query = entityManager.createQuery("from Faculty where facultyName=:facultyname and university.universityId=:universityid");
        query.setParameter("facultyname", facultyName);
        query.setParameter("universityid", university.getUniversityId());
        if (!query.getResultList().isEmpty()) {
            return (Faculty) query.getResultList().get(0);
        } else {
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Faculty> findAllFacultiesByUniversityName(String universityName) {
        Query query = entityManager.createQuery("from Faculty where university.universityName=:universityname");
        query.setParameter("universityname", universityName);
        return query.getResultList();
    }


}
